package com.study.zzedu.ui.schoolroll;

import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;

import com.androidkun.xtablayout.XTabLayout;
import com.study.zzedu.R;
import com.study.zzedu.adapter.TabAdapter;
import com.study.zzedu.adapter.TabForStuInfoAdapter;
import com.study.zzedu.base.BaseMvpActivity;
import com.study.zzedu.base.BaseView;
import com.study.zzedu.utils.ToastUtil;
import com.study.zzedu.widget.VersionInfoItem;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class SchoolRollActivity extends BaseMvpActivity<SchoolRollContract.IView, SchoolRollPresenter> implements SchoolRollContract.IView {

//    private VersionInfoItem
//            vi_stu_num,vi_name,
//            vi_major,vi_xue_center,
//            vi_date_brith,vi_nation,
//            vi_gender,vi_admission_time;

    public static JSONObject schoolRollInfo_personalInfo;//学籍信息 和 个人信息
    public static JSONArray gardeInfo;//成绩信息
    private XTabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> list;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_school_roll;
    }

    @Override
    protected SchoolRollPresenter createPresenter() {
        return new SchoolRollPresenter();
    }

    @Override
    protected SchoolRollContract.IView createView() {
        return this;
    }

    @Override
    protected void initView() {

        viewPager = findViewById(R.id.viewpager);
        tabLayout = findViewById(R.id.tab);
    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        list.add("学籍信息");
        list.add("个人信息");
        list.add("成绩信息");

         getPresenter().getUserDetailMessage(SchoolRollActivity.this);
    }

    @Override
    protected void initListener() {
        findViewById(R.id.rl_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void getData(String data) {

    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

    @Override
    public void onGetUserDetailMessage(String name) {
        //ToastUtil.showShortToast("获得学籍信息");
        try {
            JSONObject jObject = new JSONObject(name);
            JSONArray jArray = jObject.getJSONArray("data");
            if (jArray.length() > 0) {
                schoolRollInfo_personalInfo = jArray.getJSONObject(0);
//                vi_stu_num.tvInfo.setText(jArray.getJSONObject(0).getString("userCode")) ;
//                vi_name.tvInfo.setText(jArray.getJSONObject(0).getString("name")) ;
//                vi_major.tvInfo.setText(jArray.getJSONObject(0).getString("major")) ;
//                vi_xue_center.tvInfo.setText(jArray.getJSONObject(0).getString("depart")) ;
//                vi_date_brith.tvInfo.setText(jArray.getJSONObject(0).getString("birth")) ;
//                vi_nation.tvInfo.setText(jArray.getJSONObject(0).getString("nation")) ;
//                vi_gender.tvInfo.setText(jArray.getJSONObject(0).getString("sex")) ;
//                vi_admission_time.tvInfo.setText(jArray.getJSONObject(0).getString("enterDate")) ;
            }
        } catch (Exception e) {
            e.toString();
        }
        getPresenter().getUserScoreList(SchoolRollActivity.this);
    }

    @Override
    public void onGetUserScoreList(String name) {
        //ToastUtil.showShortToast("获得成绩信息");
        try {
            JSONObject jObject = new JSONObject(name);
            JSONArray jArray = jObject.getJSONArray("data");
            if (jArray.length() > 0) {
                gardeInfo = jArray;
            }
        } catch (Exception e) {
            e.toString();
        }

        /*设置Adapter*/
        viewPager.setAdapter(new TabForStuInfoAdapter(getSupportFragmentManager(), list));
        /*Tab与ViewPager绑定*/
        tabLayout.setupWithViewPager(viewPager);
    }
}